import { clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';

export function cn(...inputs) {
    return twMerge(clsx(inputs));
}

export const wordCountAndSort = (arr) => {
    const flatArr = arr.flat().map((w) => w.toUpperCase());
    const countObj = flatArr.reduce((countObj, word) => {
        countObj[word] = (countObj[word] || 0) + 1;
        return countObj;
    }, {});

    const sortedArr = Object.entries(countObj).sort((a, b) => b[1] - a[1]);
    const filteredArr = sortedArr.filter((a) => a[1] > 1);
    const result = filteredArr.map((item) => Object({ key: item[0], cnt: item[1] }));
    // console.log(result)
    // const sortedObj = filteredArr.reduce((obj, [word, count]) => {
    //     obj[word] = count;
    //     return obj;
    // }, {});

    return result;
};

export const arrayCompare = (arr1, arr2) => {
    // 创建 Map 存储小写形式到第一个数组原始值的映射
    const lowerCaseMap = new Map();
    for (const item of arr1) {
        const lower = String(item).toLowerCase();
        if (!lowerCaseMap.has(lower)) {
            lowerCaseMap.set(lower, item);
        }
    }

    // 遍历第二个数组，查找交集（使用第一个数组的原始值）
    const result = [];
    for (const item of arr2) {
        const lower = String(item).toLowerCase();
        if (lowerCaseMap.has(lower)) {
            result.push(lowerCaseMap.get(lower));
            lowerCaseMap.delete(lower); // 防止重复添加
        }
    }

    return result;
};
